package com.h3bpm.kumhosunny.common.utils;

import OThinker.H3.Portal.webservices.Entity.DataItemParam;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import net.sf.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zpf
 * @description
 * @date 2019/5/24 11:01
 */
public class JavaNetUrlRestFulClient {

    static int STATUS = 200;


    public static String startInstance(String systemCode,String secret,String workFlowCode,String userCode,String finishStart,List<DataItemParam> paramValues){
        String targetUrlAll = "http://localhost:8081/Portal/bpm-api/workflows/listButton";
        StringBuffer sb = new StringBuffer("");
        try {

            URL targetUrl = new URL(targetUrlAll);

            HttpURLConnection httpConnection = (HttpURLConnection) targetUrl.openConnection();
            httpConnection.setDoOutput(true);
            httpConnection.setRequestMethod("POST");
            httpConnection.setRequestProperty("Content-Type", "application/json");

            Map<String,Object> dataMap = new HashMap<>(16);
            dataMap.put("systemCode",systemCode);
            dataMap.put("secret",secret);
            dataMap.put("workflowCode",workFlowCode);
            dataMap.put("userCode",userCode);
            dataMap.put("finishStart",finishStart);
            dataMap.put("paramValues",paramValues);
            String input = JSONObject.fromObject(dataMap).toString();

            System.out.println(input);

            OutputStream outputStream = httpConnection.getOutputStream();
            outputStream.write(input.getBytes());
            outputStream.flush();

            if (httpConnection.getResponseCode() != STATUS) {
                throw new RuntimeException("Failed : HTTP error code : "
                        + httpConnection.getResponseCode());
            }

            BufferedReader responseBuffer = new BufferedReader(new InputStreamReader(
                    (httpConnection.getInputStream())));

            String output = null;

            while ((output = responseBuffer.readLine()) != null) {
                sb.append(output);
            }
            httpConnection.disconnect();

        } catch (MalformedURLException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();
        }
        return sb.toString();
    }

    public static void main(String[] args) throws IOException {
        String systemCode = "H3";
        String secret = "Authine";
        String workFlowCode = "listButton";
        String userCode = "wangkeke";
        String finishStart = "true";

        List<DataItemParam> list = new ArrayList<>();
        list.add(new DataItemParam("biaodanid","表单id333"));
        list.add(new DataItemParam("biaodanid2","333"));
        list.add(new DataItemParam("ObjectID","940131"));

        String jsonResult = startInstance(systemCode, secret, workFlowCode, userCode, finishStart, list);

        ObjectMapper mapper = new ObjectMapper();
        JavaType javaType = mapper.getTypeFactory()
                .constructParametricType(Map.class, String.class, Object.class);
        Map<String, Object> map1 = mapper.readValue(jsonResult, javaType);
        System.out.println(map1);

//        try {
//            String endpoint = "http://20.0.3.5:21112/Portal/WebServices/BPMService?wsdl";
//            //直接引用远程的wsdl文件
//            //以下都是套路
//            Service service = new Service();
//            Call call = (Call) service.createCall();
//            call.setTargetEndpointAddress(endpoint);
//            call.setOperationName("StartWorkflowNew");//WSDL里面描述的接口名称
//            call.addParameter("systemCode", XMLType.XSD_STRING,
//                    javax.xml.rpc.ParameterMode.IN);//接口的参数
//            call.addParameter("secret", XMLType.XSD_STRING,
//                    javax.xml.rpc.ParameterMode.IN);//接口的参数
//            call.addParameter("workFlowCode", XMLType.XSD_STRING,
//                    javax.xml.rpc.ParameterMode.IN);//接口的参数
//            call.addParameter("userCode", XMLType.XSD_STRING,
//                    javax.xml.rpc.ParameterMode.IN);//接口的参数
//            call.addParameter("finishStart", XMLType.XSD_STRING,
//                    javax.xml.rpc.ParameterMode.IN);//接口的参数
//            call.addParameter("paramValues", XMLType.XSD_STRING,
//                    javax.xml.rpc.ParameterMode.IN);//接口的参数
//            call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);//设置返回类型
//            String result = (String)call.invoke(new Object[]{systemCode,secret,workFlowCode,userCode,finishStart,paramValues});
//            //给方法传递参数，并且调用方法
//            System.out.println("result is "+result);
//        }
//        catch (Exception e) {
//            System.err.println(e.toString());
//        }
    }
}
